﻿using System;
using DC2010.Objects.Base;
using DC2010.Objects;
using DC2010.Objects.Common;
using DC2010.Objects.Data;

namespace DC2010.Effects
{
    public class I_Ressurect_Effect : EffectLogic
    {
        public I_Ressurect_Effect(EffectData ed):base(ed)
        {
            ShortName = "Resurrect";
            LongName = "Resurects party member.";
            EffectType = EffectType.Instant;
        }

        public override void Instant(BaseObject aInvoker, BaseObject aTarget)
        {
            if ((aTarget is ItemData) && (aInvoker is CreatureData))
            {
                CreatureData c = (CreatureData) aInvoker;
                CreatureData cDeadOne = null;
                ItemData iBones = (ItemData)aTarget;

                //it is called "bones" here, but technically it can mean anything...any item
                
                // Lets get party of member who puts this bones to altar
                // This can be monster party as well :)
                PartyData p = null;// Root.I.PartySystem.GetData(c.PartyId);
                
                // Are these bones related to dead party member?
                foreach (CreatureData pcd in p.MembersData)
                {
                    // we are intereted only in dead members
                    if (pcd.Flags.IsDead)
                    {
                        ObjectReference or = new ObjectReference();
                        or.ObjectType = BaseObjectType.Creature;
                        or.ReferencedId = pcd.Id;

                        cDeadOne = (CreatureData)Root.I.GameSystem.GetReferencedObject(iBones.LinkedObjects, BaseObjectType.Creature, pcd.Id);

                        if (cDeadOne != null)
                        {
                            cDeadOne = pcd;
                        }
                    }
                }

                if (cDeadOne != null)
                {
                    // resurrect creature
                    cDeadOne.Flags.IsDead = false;

                    // remove bones from system
                    Root.I.ItemSystem.RemoveData(iBones);

                    //set health, stamina and mana

                    Random r = new Random();
                    if (cDeadOne.Attributes.Health.Current < 0) cDeadOne.Attributes.Health.SetCurrent(0);
                    if (cDeadOne.Attributes.Stamina.Current < 0) cDeadOne.Attributes.Stamina.SetCurrent(0);
                    if (cDeadOne.Attributes.Mana.Current < 0) cDeadOne.Attributes.Mana.SetCurrent(0);

                    int newHealth = r.Next(cDeadOne.Attributes.Health.Max / 2);
                    int newStamina = r.Next(cDeadOne.Attributes.Stamina.Max / 2);
                    int newMana = r.Next(cDeadOne.Attributes.Mana.Max / 2);

                    cDeadOne.Attributes.Health.SetCurrent(newHealth);
                    cDeadOne.Attributes.Stamina.SetCurrent(newStamina);
                    cDeadOne.Attributes.Mana.SetCurrent(newMana);
                }
            }

            base.Instant(aInvoker, aTarget);
        }

    }
}
